﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Expression.Interactivity.Media;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            // Required to initialize variables
            InitializeComponent();
            this.Loaded += new System.Windows.RoutedEventHandler(MainPage_Loaded);
            CurrentTopLayer = pnlStrong.Parent as Grid;
            ((Grid)pnlStrong.Parent).DataContext = 1;
            ((Grid)pnlMedium.Parent).DataContext = 2;
            ((Grid)pnlLow.Parent).DataContext = 3;
        }

        private Grid CurrentTopLayer;

        private void Panel_KeyDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            Grid panel = (Grid)(sender as Path).Parent;

            if (panel.Name == CurrentTopLayer.Name)
            {
                return;
            }

            int layerLevelOfPanel = Convert.ToInt32(panel.DataContext);
            arrowAction.Begin();
            switch (panel.Name)
            {
                case "gridStrong":
                    this.strong_n_1.Begin();
                    break;
                case "gridMedium":
                    this.medium_n_1.Begin();
                    break;
                case "gridLow":
                    this.low_n_1.Begin();
                    break;
                default:
                    break;
            }
            panel.DataContext = 1;
            switch (CurrentTopLayer.Name)
            {
                case "gridStrong":
                    if (layerLevelOfPanel == 2)
                        this.strong_1_2.Begin();
                    if (layerLevelOfPanel == 3)
                        this.strong_1_3.Begin();
                    break;
                case "gridMedium":
                    if (layerLevelOfPanel == 2)
                        this.medium_1_2.Begin();
                    if (layerLevelOfPanel == 3)
                        this.medium_1_3.Begin();
                    break;
                case "gridLow":
                    if (layerLevelOfPanel == 2)
                        this.low_1_2.Begin();
                    if (layerLevelOfPanel == 3)
                        this.low_1_3.Begin();
                    break;
                default:
                    break;
            }
            CurrentTopLayer.DataContext = layerLevelOfPanel;
            CurrentTopLayer = panel;
            arrowAction.Begin();
        }

        private void btnAddEntity_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            var companyBox = new Button();
            companyBox.Style = (Style)Application.Current.Resources["DetailBox"];
            companyBox.DataContext = new CompanyInfo() { CompanyName = "New Company", Numb1 = "8", Numb2 = "2", Numb3 = "13", SubInformation = "New Company...." };
            companyBox.HorizontalAlignment = HorizontalAlignment.Right;
            companyBox.VerticalAlignment = VerticalAlignment.Top;
            companyBox.Margin = new Thickness(40, 0, 0, 0);
            //companyBox.Margin = new Thickness(100);
            spCompany.Children.Add(companyBox);
            
            
        }

        private void btnDeleteEntity_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            if (spCompany.Children.Count > 0)
            {
                spCompany.Children.RemoveAt(0);
            }
        }

        private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.

            double marginX = 5;
            double marginY = 5;

            foreach (var com in CompanyInfo.Companies)
            {
                var companyBox = new Button();
                companyBox.Style = (Style)Application.Current.Resources["DetailBox"];
                companyBox.DataContext = com;
                //companyBox.Margin = new Thickness( marginX, marginY, 0, 0 );
                //companyBox.MouseEnter += new MouseEventHandler(companyBox_MouseEnter);
                //if( companyBox.Width + marginX > gridCompany.Margin.Right  )
                //{
                //    marginY += companyBox.Margin.Bottom - companyBox.Margin.Top;
                //    marginX = 5;
                //}
                companyBox.HorizontalAlignment = HorizontalAlignment.Right;
                companyBox.VerticalAlignment = VerticalAlignment.Top;
                companyBox.Margin = new Thickness(0,0, 40, 40);
                //companyBox

                //companyBox.Margin = new Thickness(gridCompany.Margin.Left + marginX, gridCompany.Margin.Top + marginY,
                //                                  gridCompany.Margin.Left + marginX + companyBox.Width,
                //                                  gridCompany.Margin.Top + marginY + companyBox.Height);
                //marginX += 80;
                
                companyBox.Click += new RoutedEventHandler(companyBox_Click);
                spCompany.Children.Add(companyBox);
            }
        }

        void companyBox_Click(object sender, RoutedEventArgs e)
        {
            Storyboard boxStory = new Storyboard();
            
        }
    }
}